package com.custom.jdbc.handler;

import com.custom.comm.utils.AssertUtil;
import com.custom.comm.utils.StrUtils;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 类型处理
 * @author  Xiao-Bai
 * @since  2022/11/12 0:11
 */
@SuppressWarnings("unchecked")
public abstract class AbstractTypeHandler<T> implements TypeHandler<T> {


    /**
     * 转换成数字类型
     */
    protected Number castNumber(Object val) {
        if (val instanceof CharSequence) {
            String s = String.valueOf(val);
            s = s.trim();
            if (StrUtils.isBlank(s)) {
               return new BigDecimal(0);

            }else throw new NumberFormatException("Cannot convert to numeric type");
        }
        AssertUtil.allowed(val instanceof Number, "Cannot convert to numeric type");
        return (Number) val;
    }

    public T getTypeValue(ResultSet rs, String column) throws SQLException {
        return (T) rs.getObject(column);
    }

}
